﻿

<div id="editContentModal"  class="modal fade in" aria-hidden="true">
    <div class="" style="width:1000px;">
        <div class="box box-info">
            <form class="form-horizontal">
                <input type="hidden" id="OddId" />
                <div id="contents">

                    <div class="box-body odd-bet-row">
                        <input class="Id" type="hidden" />
                        <div class="form-group col-md-6 col-sm-6" >
                            <div class="box-body" style="height:640px;">
                                <div  style="max-height:600px;overflow:auto; ">
                                    <div class="matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛1</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match0" dIndex="0" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="0" />
                                            </div>
                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table0" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"> </td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#" bOddId="" data-target="had" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="hhad" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="half" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                    <div class=" matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛2</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match1" dIndex="1" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="1" />
                                            </div>


                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table1" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="had" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="hhad" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>

                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="half" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                    <div class=" matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛3</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match2" dIndex="2" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="2" />
                                            </div>


                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table2" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="had" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#" bOddId="" data-target="hhad" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#" bOddId="" data-target="half" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>

                                    <div class="matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛4</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match3" dIndex="3" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="3" />
                                            </div>
                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table3" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"> </td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="had" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="hhad" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="half" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                    <div class=" matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛5</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match4" dIndex="4" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="4" />
                                            </div>


                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table4" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="had" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#" bOddId="" data-target="hhad" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="half" class="next" matchId="" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                    <div class="matchData">
                                        <div>
                                            <label class="col-sm-3 control-label" for="">比赛6</label>
                                            <div class="col-sm-9 form-group">
                                                <select id="match5" dIndex="5" class="form-control match" style="padding:0px;width:300px;"></select>
                                                <input type="checkbox" class="checkboxs dan" value="5" />
                                            </div>


                                        </div>
                                        <div>
                                            <label class="col-sm-3 control-label" for=""></label>
                                            <div class="col-sm-9 form-group">
                                                <table id="table5" class="table table-condensed table-cell table-bordered text-center" style="padding:0px;width:300px;">
                                                    <tr zIndex="1">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="had" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="2">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#"  bOddId="" data-target="hhad" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>
                                                    <tr zIndex="3">
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td></td>
                                                        <td class="odd"></td>
                                                        <td>
                                                            <a href="#" bOddId="" data-target="half" matchId="" class="next" pageIndex="1">next</a>&nbsp;&nbsp;
                                                            <span></span>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                </div>

                                
                            </div>
                            <div class="box-body">
                                <label class="col-sm-3 control-label" for="">过关方式</label>
                                <div class="col-sm-9 form-group" style="width:150px;">

                                    <div class="checkbox input-group-addon">
                                        <label>
                                            <input type="checkbox" value="1" class="createOdd">单关
                                        </label>
                                    </div>
                                    <div class="checkbox input-group-addon">
                                        <label>
                                            <input type="checkbox" value="2" class="createOdd" />2串1
                                        </label>

                                    </div>
                                    <div class="checkbox input-group-addon">
                                        <label>
                                            <input type="checkbox" value="3" class="createOdd" />3串1
                                        </label>
                                    </div>
                                </div>

                            </div>
                        </div>
                        <div class="form-group col-md-6 col-sm-6">
                            <div class="box-body" style="height:640px;">
                                <div style="max-height:600px;overflow:auto;">
                                    <table id="dataTable" class="table table-bordered table-striped text-center" align="center">
                                        <tr>
                                            <th>
                                            </th>
                                            <th>
                                                投注内容
                                            </th>
                                            <th>
                                                赔率
                                            </th>
                                            <th>
                                                注数
                                            </th>
                                            <th>
                                                奖金
                                            </th>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                <input type="checkbox" style="height:18px;" class="checkbox" />
                                            </td>
                                            <td>
                                                周二001胜1.25|周二002平2.9
                                            </td>
                                            <td>
                                                <input type="text" value="4" style="width:50px;" />
                                            </td>
                                            <td>
                                                19.8
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                <input type="checkbox" style="height:18px;" class="checkbox" />
                                            </td>
                                            <td>
                                                周二001胜1.25|周二002平2.9
                                            </td>
                                            <td>
                                                <input type="text" value="4" style="width:50px;" />
                                            </td>
                                            <td>
                                                19.8
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                            <div class="box-body">
                                <input  type="hidden" id="oddArrString"/>
                                <div class="form-group col-sm-1"> &nbsp;</div>
                                <div class="form-group col-sm-4">
                                    <div class="input-group">

                                        <input type="text" class="form-control" id="a1" placeholder="单倍金额">
                                        <div class="input-group-addon">元</div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-1"> &nbsp;</div>
                                <div class="form-group col-sm-4">

                                    <div class="input-group">
                                        <input type="text" class="form-control" id="a2" placeholder="倍数">
                                        <div class="input-group-addon">倍</div>
                                    </div>

                                </div>
                                <div class="form-group col-sm-1"> &nbsp;</div>
                                <div class="form-group col-sm-4">

                                    <div class="input-group">
                                        <input type="text" class="form-control" id="a3" placeholder="总计" readonly="readonly">
                                        <div class="input-group-addon">元</div>
                                    </div>

                                </div>
                            </div>
                        </div>

                    </div>

                </div>

                <div class="box-footer">
                    <div class="pull-right box-tools">
                        <button id="btnSaveContent" class="btn btn-sm btn-primary" type="button">保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

</div>
<script>
    //所有的比赛信息
    var matchData = [];
    var selectedMatchIds = [];
    //选中的一场投注单
    var selectedSingleOdd = {};
    var selectedMatchs = [];
    var betDetailOdds = [];
    $(function () {

        $('#btnSaveContent').click(SaveMatchContents);

        $(".match").select2(
            {
                placeholder: "请选择"

            });
        $(".odd").click(function () {

            var html = $(this).html();
            if (html != "") {
                if ($(this).attr("class").indexOf("selected") < 0) {
                    $(this).addClass("selected");

                } else {

                    $(this).removeClass("selected");

                }
            }
        })

        $(".match").change(function () {
            if (null != $(this).val()) {
                var index = parseInt($(this).attr("dIndex"));
                if (selectedMatchIds[index] != $(this).val() && parseInt($(this).val()) > 0) {
                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "/JCMatch/GetMatchsById?id=" + $(this).val() + "&_t=" + new Date().getTime(),
                        success: function (d) {
                            if (undefined != d && null != d.data) {
                                selectedMatchs[index] = d.data;

                                var brqOdd = d.data.jcHadOddsList.Where(function (x) { return x.fixedOdds == ""; }).OrderBy(function (x) { return x.releaseTime });
                                var rqOdd = d.data.jcHadOddsList.Where(function (x) { return x.fixedOdds != ""; }).OrderBy(function (x) { return x.releaseTime });

                                var tr1 = $("#table" + index + " tr:eq(0)");
                                //tr1.attr("jchadoddid", brqOdd.id);
                                $("#table" + index + " tr td").removeClass("selected");
                                var td1 = $("#table" + index + " tr:eq(0) td");

                                if (null != brqOdd && brqOdd.Count() > 0) {
                                    //$(td1[0]).html(brqOdd[0].fixedOdds);
                                    //$(td1[1]).html(brqOdd[0].homeOdds);
                                    //$(td1[2]).html(brqOdd[0].drawOdds);
                                    //$(td1[3]).html(brqOdd[0].awayOdds);
                                    //$(td1[4]).find("a").attr("pageIndex", 0);
                                    //$(td1[4]).find("a").attr("matchId", d.data.matchId);


                                    WriteTdValue(td1, brqOdd[brqOdd.length - 1], d.data.matchId, brqOdd.length - 1, brqOdd.length);
                                }
                                var tr2 = $("#table" + index + " tr:eq(1)");
                                //tr2.attr("jchadoddid", rqOdd.id)
                                var td2 = $("#table" + index + " tr:eq(1) td");
                                if (null != rqOdd && rqOdd.Count() > 0) {
                                    //$(td2[0]).html(rqOdd[0].fixedOdds);
                                    //$(td2[1]).html(rqOdd[0].homeOdds);
                                    //$(td2[2]).html(rqOdd[0].drawOdds);
                                    //$(td2[3]).html(rqOdd[0].awayOdds);
                                    //$(td2[4]).find("a").attr("pageIndex", 0);
                                    //$(td2[4]).find("a").attr("matchId", d.data.matchId);
                                    WriteTdValue(td2, rqOdd[rqOdd.length - 1], d.data.matchId, rqOdd.length-1, rqOdd.length);
                                }


                                var tr3 = $("#table" + index + " tr:eq(2)");
                                var hafuOdd = d.data.jchafuOddsList.OrderBy(function (x) { return x.releaseTime })[d.data.jchafuOddsList.length-1];
                                var td3 = $("#table" + index + " tr:eq(2) td");
                                if (null != d.data.jchafuOddsList && d.data.jchafuOddsList.length > 0) {
                                    $(td3[0]).html(hafuOdd.fixedOdds);
                                    $(td3[1]).html(hafuOdd.hh);
                                    $(td3[2]).html("");
                                    $(td3[3]).html(hafuOdd.aa);
                                    $(td3[4]).find("a").attr("pageIndex", d.data.jchafuOddsList.length - 1);
                                    $(td3[4]).find("a").attr("matchId", d.data.matchId);
                                    $(td3[4]).find("a").attr("bOddId", hafuOdd.oddId);
                                    $(td3[4]).find("span").html((d.data.jchafuOddsList.length) + "/" + d.data.jchafuOddsList.length);
                                }

                            }
                        }
                    })

                }
                selectedMatchIds[index] = $(this).val();
            }
        });

        $("a.next").click(function () {
            var value = $(this).attr("data-target");
            var matchId = parseInt($(this).attr("matchId"));
            var pageIndex = parseInt($(this).attr("pageIndex"));
            if (matchId > 0) {
                var match = selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; });
                if (null != match && null != match.jcHadOddsList && match.jcHadOddsList.length > 0) {
                    //不让球赔率
                    if (value == "had") {
                        var braOdds = match.jcHadOddsList.Where(function (x) { return x.fixedOdds == "" });
                        braOdds = braOdds.OrderBy(function (x) { return x.releaseTime });
                        pageIndex--;
                        if (pageIndex < 0) { pageIndex = braOdds.length-1; }

                        var td = $(this).parent().parent().find("td");
                        
                        WriteTdValue(td, braOdds[pageIndex], matchId, pageIndex, braOdds.length);

                    }
                    //让球赔率
                    else if (value == "hhad") {
                        var rqOdds = match.jcHadOddsList.Where(function (x) { return x.fixedOdds != "" });
                        rqOdds = rqOdds.OrderBy(function (x) { return x.releaseTime });
                        pageIndex--;
                        if (pageIndex < 0) { pageIndex = rqOdds.length-1; }
                        var td = $(this).parent().parent().find("td");

                        WriteTdValue(td, rqOdds[pageIndex], matchId, pageIndex, rqOdds.length);
                    }
                    //半全场
                    else if (value = "half") {
                        var hafuOdds = match.jchafuOddsList.OrderBy(function (x) { return x.releaseTime });
                        var td3 = $(this).parent().parent().find("td");
                        pageIndex--;
                        if (pageIndex < 0) { pageIndex = hafuOdds.length - 1; }
                        var hafuOdd = hafuOdds[pageIndex];

                        $(td3[0]).html(hafuOdd.fixedOdds);
                        $(td3[1]).html(hafuOdd.hh);
                        $(td3[2]).html("");
                        $(td3[3]).html(hafuOdd.aa);
                        $(td3[4]).find("a").attr("pageIndex", pageIndex);
                        $(td3[4]).find("a").attr("matchId", hafuOdd.matchId);
                        $(td3[4]).find("a").attr("bOddId", hafuOdd.oddId);
                        $(td3[4]).find("span").html((pageIndex+1) + "/" + hafuOdds.length);

                    }

                }

            }
            return false;
        });

        $(".createOdd").change(function () {
            var arr = [];
            $.each($(".createOdd:checked"), function (i, n) {
                arr.push($(n).val());
            });

            var smatchs = GetSMatchData();

            var oddarr = new Array();
            for (var i = 0; i < arr.length; i++) {
                if (parseInt(arr[i]) == 1) {
                    var od = MakeBettingDanGuanDetails(smatchs);
                    oddarr = oddarr.concat(od);
                } else if (parseInt(arr[i]) == 2) {
                    var od = MakeBetting2X1Details(smatchs);
                    oddarr = oddarr.concat(od);
                } else {
                    var od = MakeBetting3X1Details(smatchs);
                    oddarr = oddarr.concat(od);
                }
            }
            betDetailOdds = new Array();
            $.each(oddarr, function (i, n) {
                var sp = 1;
                $.each(n, function (f, g) {
                    sp = sp * g.SelectedOdds;
                });

                var bet = {
                    Index: i,
                    Checked: true,
                    Content: getContentString(n),
                    SP: sp.toFixed(2),
                    BetCount: 0,
                    BetDetailOddContents: n
                }
                betDetailOdds.push(bet);
            });
            WriteOddTableData();


        });


        $(document).delegate(".select-odd", "change", function () {

            var index = $(this).val();
            if ($(this).is(":checked")) {
                $(this).parent().parent().find("input[type='text']").removeAttr("disabled");
                betDetailOdds.FirstOrDefault(function (x) { return x.Index == index }).Checked = true;

            } else {

                $(this).parent().parent().find("input[type='text']").attr("disabled", "disabled");
                betDetailOdds.FirstOrDefault(function (x) { return x.Index == index }).Checked = false;
            }

        });
        $(document).delegate("#a1", "input propertychange", function () {

            var money = parseInt( $(this).val());
            var beishu = parseInt($("#a2").val());

            $("#a3").val(money * beishu);
            AverageCapital(money);
            WriteOddTableData();
        });
        $(document).delegate("#a2", "input propertychange", function () {

            var money = parseInt($("#a1").val());
            var beishu = parseInt($("#a2").val());

            $("#a3").val(money * beishu);
            
        });


        $(document).delegate(".count", "input propertychange", function () {

            var count = 0;
            $.each($(".count"), function (i, m) {
                count += parseInt($(m).val());
            });
            $("#a1").val(count * 2);

            var beishu = parseInt($("#a2").val());
            var money = parseInt($("#a1").val());
            $("#a3").val(money * beishu);
        });

    });


    //投注的平均优化
    function AverageCapital(money) {
        var arr = betDetailOdds.Where(function (x) { return x.Checked == true; });
        if (undefined != arr && null != arr && arr.length > 0) {
            money = money / 2;
            var count= money;
            var k = 0;
            for (var i = 0; i < arr.length; i++) {
                k += (1 / arr[i].SP);
            }
            k = 1 / k;
            $.each(arr, function (i, n) {
                n.BetCount = Math.round(money * k / n.SP);
                count = count - n.BetCount; 
            });
            
            if (count != 0) {
                var index = 0;
                if (count > 0) {
                    index = 1;
                    for (var i = count; i > 0; i -= index) {
                        var valueArr = new Array();
                        var ci = 0;
                        var cab = 1000000000;
                        var agvBonus = arr.Average(function (x) { return x.Bonus; });
                        $.each(arr, function (t, n) {
                            var c = DeepCopy(arr);
                            c[t].BetCount += index;
                            c[t].Bonus = c[t].BetCount * 2 * c[t].SP;
                            var cAvgBonus = c.Average(function (x) { return x.Bonus; });
                            var cabs = 0;
                            $.each(c, function (e, r) {
                                cabs += Math.abs(r.Bonus - cAvgBonus);
                            });
                            if (cab > cabs) {
                                cab = cabs;
                                ci = t;
                            }
                        });

                        arr[ci].BetCount += index;
                        arr[ci].Bonus = arr[ci].BetCount * 2 * arr[ci].SP;
                    }
                } else {
                    index = -1;
                    for (var i = count; i < 0; i -= index) {
                        var valueArr = new Array();
                        var ci = 0;
                        var cab = 1000000000;
                        var agvBonus = arr.Average(function (x) { return x.Bonus; });
                        $.each(arr, function (t, n) {
                            var c = DeepCopy(arr);
                            c[t].BetCount += index;
                            c[t].Bonus = c[t].BetCount * 2 * c[t].SP;
                            var cAvgBonus = c.Average(function (x) { return x.Bonus; });
                            var cabs = 0;
                            $.each(c, function (e, r) {
                                cabs += Math.abs(r.Bonus - cAvgBonus);
                            });
                            if (cab > cabs) {
                                cab = cabs;
                                ci = t;
                            }
                        });

                        arr[ci].BetCount += index;
                        arr[ci].Bonus = arr[ci].BetCount * 2 * arr[ci].SP;
                    }

                }
                
               
            }

            //$("#oddArrString").val(JSON.stringify(arr));
        }
    }

    ///深度拷贝数组
    function DeepCopy(o) {
        if (o instanceof Array) {
            var n = [];
            for (var i = 0; i < o.length; ++i) {
                n[i] = DeepCopy(o[i]);
            }
            return n;

        } else if (o instanceof Object) {
            var n = {}
            for (var i in o) {
                n[i] = DeepCopy(o[i]);
            }
            return n;
        } else {
            return o;
        }
    }



    function WriteOddTableData() {
        $("#dataTable tr:gt(0)").remove();
        if (betDetailOdds.length > 0) {
            $.each(betDetailOdds, function (i, n) {
                var ss = "<tr>";
                ss += "<td><input type='checkbox' " + (n.Checked == true ? " checked='checked'" : "") + " class='select-odd' value='" + n.Index + "'></td>";
                ss += "<td class='text-left'>" + n.Content + "</td>";
                ss += "<td class='text-left'>" + (n.SP * 2).toFixed(2) / 2 + "</td>";
                ss += "<td class='text-right'><input type='text' class='count' style='width:40px;' " + (n.Checked != true ? " disabled='disabled'" : "") + " value='" + n.BetCount + "'></td>";
                ss += "<td class='text-right'>" + ((n.SP * 2).toFixed(2) * n.BetCount).toFixed(0) + "</td>";
                ss += "</tr>";
                $("#dataTable").append(ss);
            });
        }
    }



    function getContentString(odd) {
        var s = "";
        $.each(odd, function (i, n) {
            s += n.WeekName + (n.FixOdds == "" ? "" : "让") + n.FixOdds + MatchResult.FirstOrDefault(function (x) { return x.id == n.SelectedResult }).text.replace("让", "") + n.SelectedOdds;
            if (i < odd.length - 1) {
                s += '|';
            }

        });
        return s;

    }


    ///获取选择的比赛投注项
    function GetSMatchData() {

        var row = $(".matchData");
        var smatchs = new Array();
        $.each(row, function (i, n) {
            if ($(n).find(".match").val() != "") {
                var matchId = parseInt($(n).find(".match").val());
                var smatch = {
                    matchId: matchId,
                    dan: ($(n).find(".dan").get(0).checked ? true : false),
                    selectedOdds: []
                }
                var trs = $(n).find("table tr");
                $.each(trs, function (j, k) {
                    
                    if (j > 1) {
                        var tds = $(k).find("td");
                        if ($(tds[1]).hasClass("selected")) {
                            var odd = {
                                SelectedMatchId: matchId,
                                FixOdds: $(tds[0]).html(),
                                WeekName: selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; }).matchNumber,
                                SelectedResult: 7,
                                SelectedOdds: parseFloat($(tds[1]).html().trim()),
                                LotteryResults: -1
                            };
                            smatch.selectedOdds.push(odd);
                        }

                        if ($(tds[3]).hasClass("selected")) {
                            var odd = {
                                SelectedMatchId: matchId,
                                FixOdds: $(tds[0]).html(),
                                WeekName: selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; }).matchNumber,
                                SelectedResult: 8,
                                SelectedOdds: parseFloat($(tds[3]).html().trim()),
                                LotteryResults: -1
                            };
                            smatch.selectedOdds.push(odd);
                        }

                    } else {
                        var tds = $(k).find("td");
                        if ($(tds[1]).hasClass("selected")) {
                            var odd = {
                                SelectedMatchId: matchId,
                                FixOdds: $(tds[0]).html(),
                                WeekName: selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; }).matchNumber,
                                SelectedResult: ($(tds[0]).html() == "" ? 1 : 4),
                                SelectedOdds: parseFloat($(tds[1]).html().trim()),
                                LotteryResults: -1
                            };
                            smatch.selectedOdds.push(odd);
                        }
                        if ($(tds[2]).hasClass("selected")) {
                            var odd = {
                                SelectedMatchId: matchId,
                                FixOdds: $(tds[0]).html(),
                                WeekName: selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; }).matchNumber,
                                SelectedResult: ($(tds[0]).html() == "" ? 2 : 5),
                                SelectedOdds: parseFloat($(tds[2]).html().trim()),
                                LotteryResults: -1
                            };
                            smatch.selectedOdds.push(odd);
                        }
                        if ($(tds[3]).hasClass("selected")) {
                            var odd = {
                                SelectedMatchId: matchId,
                                FixOdds: $(tds[0]).html(),
                                WeekName: selectedMatchs.FirstOrDefault(function (x) { return x.matchId == matchId; }).matchNumber,
                                SelectedResult: ($(tds[0]).html() == "" ? 3 : 6),
                                SelectedOdds: parseFloat($(tds[3]).html().trim()),
                                LotteryResults: -1
                            };
                            smatch.selectedOdds.push(odd);
                        }
                    }
                });

                if (smatch.selectedOdds.length > 0) {
                    smatchs.push(smatch);
                }
            }

        });
        return smatchs;
    }


    function MakeBettingDanGuanDetails(smatchs) {
        var odds = new Array();
        for (var i = 0; i < smatchs.length; i++) {
            for (var c = 0; c < smatchs[i].selectedOdds.length; c++) {
                odds.push([smatchs[i].selectedOdds[c]]);
            }
        }
        return odds;
    }

    function MakeBetting2X1Details(smatchs) {
        var odds = new Array();
        var danMatch = smatchs.Where(function (x) { return x.dan == true; });
        if (danMatch.length > 0) {
            noDanMatch = smatchs.Where(function (x) { return x.dan == false; });

            for (var i = 0; i < danMatch.length; i++) {
                for (var j = i + 1; j < danMatch.length; j++) {
                    if (danMatch[i].matchId != danMatch[j].matchId) {
                        for (var c = 0; c < danMatch[i].selectedOdds.length; c++) {
                            for (var b = 0; b < danMatch[j].selectedOdds.length; b++) {

                                var singleArr = new Array();
                                singleArr.push(danMatch[i].selectedOdds[c]);
                                singleArr.push(danMatch[j].selectedOdds[b]);
                                odds.push(singleArr);
                            }
                        }
                    }
                }
                for (var j = 0; j < noDanMatch.length; j++) {
                    if (danMatch[i].matchId != noDanMatch[j].matchId) {
                        for (var c = 0; c < danMatch[i].selectedOdds.length; c++) {
                            for (var b = 0; b < noDanMatch[j].selectedOdds.length; b++) {

                                var singleArr = new Array();
                                singleArr.push(danMatch[i].selectedOdds[c]);
                                singleArr.push(noDanMatch[j].selectedOdds[b]);
                                odds.push(singleArr);
                            }
                        }
                    }
                }
            }
        } else {
            for (var i = 0; i < smatchs.length; i++) {
                for (var j = i + 1; j < smatchs.length; j++) {
                    if (smatchs[i].matchId != smatchs[j].matchId) {
                        for (var c = 0; c < smatchs[i].selectedOdds.length; c++) {
                            for (var b = 0; b < smatchs[j].selectedOdds.length; b++) {
                                var singleArr = new Array();
                                singleArr.push(smatchs[i].selectedOdds[c]);
                                singleArr.push(smatchs[j].selectedOdds[b]);
                                odds.push(singleArr);
                            }
                        }
                    }

                }
            }

        }
        return odds;

    }


    function MakeBetting3X1Details(smatchs) {
        if (smatchs.length < 3) {
            return [];
        }
        var odds = new Array();
        for (var a = 0; a < smatchs.length; a++)
        { 
            var d = smatchs[a];
            for (var i = 0; i < d.selectedOdds.length; i++) {
                for (var b = a + 1; b < smatchs.length; b++) {
                    var e = smatchs[b];
                    for (var j = 0; j < e.selectedOdds.length; j++) {
                        for (var c = b + 1; c < smatchs.length; c++) {
                            
                            var f = smatchs[c];
                            for (var k = 0; k < f.selectedOdds.length; k++) {
                                var singleArr = new Array();
                                singleArr.push(d.selectedOdds[i]);
                                singleArr.push(e.selectedOdds[j]);
                                singleArr.push(f.selectedOdds[k]);
                                odds.push(singleArr);

                            }
                        }
                    }
                }
            }
        }
        return odds;



        //for (var i = 0; i < smatchs[0].selectedOdds.length; i++) {
        //    for (var j = 0; j < smatchs[1].selectedOdds.length; j++) {

        //        for (var k = 0; k < smatchs[2].selectedOdds.length; k++) {
        //            var singleArr = new Array();
        //            singleArr.push(smatchs[0].selectedOdds[i]);
        //            singleArr.push(smatchs[1].selectedOdds[j]);
        //            singleArr.push(smatchs[2].selectedOdds[k]);
        //            odds.push(singleArr);
        //        }
        //    }
        //}
       // return odds;
    }

    function WriteTdValue(tds, hadOdd, matchId, pageIndex,count) {
        $(tds[0]).html(hadOdd.fixedOdds);
        $(tds[1]).html(hadOdd.homeOdds);
        $(tds[2]).html(hadOdd.drawOdds);
        $(tds[3]).html(hadOdd.awayOdds);
        $(tds[4]).find("a").attr("pageIndex", pageIndex);
        $(tds[4]).find("a").attr("matchId", matchId);
        $(tds[4]).find("a").attr("bOddId", hadOdd.oddId);

        $(tds[4]).find("span").html((pageIndex + 1) + "/" + count);
    }


    function isRepeat(arr) {
        var hash = {};
        for (var i in arr) {
            if (hash[arr[i]]) {
                return true;
            }
            hash[arr[i]] = true;
        }
        return false;

    }
    ///获取比赛信息
    function GetMacthsData(time) {
        $.ajax({
            type: "GET",
            async: false,
            url: "/JCMatch/GetSingleWeekMatchData?startTime=" + time + "&_t=" + new Date().getTime(),
            success: function (d) {
                matchData = d.data.OrderBy(function (x) { return x.text });
                //CreateMatchOption("#matchs1");
            }
        })
    }

    function CreateMatchOption(selector) {
        $.each(matchData, function (i, item) {
            var value = item.matchId;
            var tr = "<option value='" + value + "'>" + item.matchNumber + ' ' + item.homeTeamName + '  VS  ' + item.awayTeamName + "</option>";
            $(selector).append(tr);
            $(selector).select2();
        });
    }


    function GetSelectedMatchData(ids) {
        var dataValue = { ids: ids };
        $.ajax({
            type: "POST",
            data: dataValue,
            url: "/JCMatch/GetMatchsByIds?_t=" + new Date().getTime(),
            success: function (d) {
                if (undefined != d && d.data.length > 0) {
                    $.each(d.data, function (i, n) {

                        $("select.match").eq(i).val( [n.matchId]).trigger("change");
                        var dd = selectedSingleOdd.oddSelectedMatchList.FirstOrDefault(function (x) { return x.selectedMatchId == n.matchId });

                        if (dd.dan)
                        { 
                            $(".dan").eq(i).prop("checked", true);
                        }

                        //var brqOdd = n.jcHadOddsList.FirstOrDefault(function (x) { return x.oddId == dd.selectedJCHadOddId && x.fixedOdds == "" });

                        var brqOdds = n.jcHadOddsList.Where(function (x) { return x.fixedOdds == "" }).OrderBy(function (x) { return x.releaseTime });
                        var brqOdd = brqOdds.FirstOrDefault(function (x) { return x.oddId == dd.selectedJCHadOddId; })

                        if (null != brqOdd) {  
                            var tr1 = $("#table" + i + " tr:eq(0)");
                            tr1.attr("jchadoddid", brqOdd.id);
                            var td1 = $("#table" + i + " tr:eq(0) td");
                            if (null != brqOdd) {
                                $(td1[0]).html(brqOdd.fixedOdds);
                                var oddValueArr = dd.selectedJCHadOdds.split(",");
                                if (oddValueArr.contains("1"))
                                {
                                    $(td1[1]).addClass("selected");
                                }
                                if (oddValueArr.contains("2")) {
                                    $(td1[2]).addClass("selected");
                                }
                                if (oddValueArr.contains("3")) {
                                    $(td1[3]).addClass("selected");
                                }
                                $(td1[1]).html(brqOdd.homeOdds);
                                $(td1[2]).html(brqOdd.drawOdds);
                                $(td1[3]).html(brqOdd.awayOdds);
                                $(td1[4]).find("a").attr("pageindex", brqOdds.indexOf(brqOdd));

                                $(td1[4]).find("span").html((brqOdds.indexOf(brqOdd) + 1) + "/" + brqOdds.length);
                            }
                        }

                        //var rqOdd = n.jcHadOddsList.FirstOrDefault(function (x) { return x.oddId == dd.selectedJCHHadOddId && x.fixedOdds != "" });
                        var rqOdds = n.jcHadOddsList.Where(function (x) { return  x.fixedOdds != "" });

                        var rqOdd = rqOdds.FirstOrDefault(function (x) { return x.oddId == dd.selectedJCHHadOddId; })

                        if (null != rqOdd) {
                            var oddValueArr = dd.selectedJCHHadOdds.split(",");
                            var tr2 = $("#table" + i + " tr:eq(1)");
                            tr2.attr("jchadoddid", rqOdd.id)
                            var td2 = $("#table" + i + " tr:eq(1) td");
                            if (null != rqOdd ) {

                                if (oddValueArr.contains("4")) {
                                    $(td2[1]).addClass("selected");
                                }
                                if (oddValueArr.contains("5")) {
                                    $(td2[2]).addClass("selected");
                                }
                                if (oddValueArr.contains("6")) {
                                    $(td2[3]).addClass("selected");
                                }

                                $(td2[0]).html(rqOdd.fixedOdds);
                                $(td2[1]).html(rqOdd.homeOdds);
                                $(td2[2]).html(rqOdd.drawOdds);
                                $(td2[3]).html(rqOdd.awayOdds);
                                $(td2[4]).find("a").attr("pageindex", rqOdds.indexOf(rqOdd));
                                $(td2[4]).find("span").html((rqOdds.indexOf(rqOdd) + 1) + "/" + rqOdds.length);
                            }
                        }


                        var hafuOdds = n.jchafuOddsList.OrderBy(function (x) { return x.releaseTime });
                        var hafuOdd = n.jchafuOddsList.FirstOrDefault(function (x) { return x.oddId == dd.selectedJCHAFUOddId; });
                        if (null != hafuOdd)
                        {
                            var oddValueArr = dd.selectedJCHAFUOdds.split(",");
                            var tr3 = $("#table" + i + " tr:eq(2)");
                            tr3.attr("jchadoddid", hafuOdd.id)
                            var td3 = $("#table" + i + " tr:eq(2) td");
                            if (null != hafuOdd) {

                                if (oddValueArr.contains("7")) {
                                    $(td3[1]).addClass("selected");
                                }
                                if (oddValueArr.contains("8")) {
                                    $(td3[3]).addClass("selected");
                                }
                                $(td3[0]).html(hafuOdd.fixedOdds);
                                $(td3[1]).html(hafuOdd.homeOdds);
                                $(td3[2]).html(hafuOdd.drawOdds);
                                $(td3[3]).html(hafuOdd.awayOdds);
                                $(td3[4]).find("a").attr("pageindex", hafuOdds.indexOf(hafuOdd));
                                $(td3[4]).find("span").html((hafuOdds.indexOf(hafuOdd) + 1) + "/" + hafuOdds.length);
                            }
                        }
                    }); 
                }
            }
        });
    }

    ///编辑投注内容
    function EditOddContent(selector) {
        var ids = [];
        $(".checkboxs").each(function () {
            if ($(this).prop("checked") == true) {
                ids.push($(this).val());
            }
        });
        if (ids.length == 0) {
            layer.tips("请选择要编辑的记录", selector);
            return;
        };
        if (ids.length > 1) {
            layer.tips("不能同时编辑多条记录", selector);
            return;
        }
        var id = ids[0];

        $(".checkboxs").prop("checked", false);
        selectedOddId = id;
        //$(".match").val([]).trigger("change");
        $("#dataTable tr:gt(0)").remove();
        $("#a1").val("");
        $("#a2").val("1");
        $(".createOdd[value='1'],.dan").prop("checked",false);

        $("#table0,#table1,#table2,#table3,#table4,#table5").find("td").removeClass("selected");
        $.each($("#table0,#table1,#table2,#table3,#table4,#table5").find("tr"), function (a, b) {
            $(b).find("td:lt(4)").html("");
        });
        $(".createOdd").removeAttr("checked");
        //$(".odd-bet-row input[type='text']").val("");
        //$(".odd-bet-row").fadeIn();
        $.ajax({
            type: "Get",
            url: "/Odd/Get?id=" + id + "&_t=" + new Date().getTime(),

            success: function (d) {

                if (null != d && undefined != d) {
                    var data = d.data;
                    selectedSingleOdd = data;
                    GetMacthsData(data.createTime);

                    var selector = $('select.match');
                    selector.empty().select2(
                        {
                            data: matchData,
                            placeholder: "请选择"
                        });
                    selector.val([0]).trigger("change");
                    if (selectedSingleOdd.oddSelectedMatchList.length == 0) {
                        $(".createOdd[value='1'],.dan").prop("checked",false);

                    } else {

                        var selectedMatchs = [];
                        var ids = selectedSingleOdd.oddSelectedMatchList.Select(function (x) { return x.selectedMatchId });
                     
                        //加载选择的比赛选项
                        GetSelectedMatchData(ids);
                        betDetailOdds = ConvertToBetDetailOdds(selectedSingleOdd.betDetailOddList);

                        if (selectedSingleOdd.betDetailOddList.Count(function (x) { return x.betDetailOddContentsList.length == 1 }) > 0)
                        { 
                            $(".createOdd[value='1']").prop("checked", true);
                        }
                        if (selectedSingleOdd.betDetailOddList.Count(function (x) { return x.betDetailOddContentsList.length == 2 }) > 0) {
                            $(".createOdd[value='2']").prop("checked", true);
                        }
                        if (selectedSingleOdd.betDetailOddList.Count(function (x) { return x.betDetailOddContentsList.length == 3 }) > 0) {
                            $(".createOdd[value='3']").prop("checked", true);
                        }


                        var counts = selectedSingleOdd.betDetailOddList.Select(function (x) { return x.betCount; });
                       
                        var gbs = data.divisor;

                        $.each(betDetailOdds, function (e, r) {
                            r.BetCount = parseInt(r.BetCount) / gbs;
                        });

                        $("#a2").val(gbs);
                        $("#a1").val(2 * betDetailOdds.Sum(function (x) { return x.BetCount }));

                        var beishu = parseInt($("#a2").val());
                        var money = parseInt($("#a1").val());
                        $("#a3").val(money * beishu);
                        //右边投注选项
                        $("#dataTable tr:gt(0)").remove();
                        if (betDetailOdds.length > 0) {
                            $.each(betDetailOdds, function (i, n) {
                                var ss = "<tr>";
                                ss += "<td><input type='checkbox' " + (n.Checked == true ? " checked='checked'" : "") + " class='select-odd' value='" + n.Index + "'></td>";
                                ss += "<td class='text-left'>" + n.Content + "</td>";
                                ss += "<td class='text-left'>" + (n.SP * 2).toFixed(2) / 2 + "</td>";
                                ss += "<td class='text-right'><input type='text' class='count' style='width:60px;' " + (n.Checked != true ? " disabled='disabled'" : "") + " value='" + n.BetCount + "'></td>";
                                ss += "<td class='text-right'>" + ((n.SP * 2).toFixed(2) * n.BetCount).toFixed(0) + "</td>";
                                ss += "</tr>";
                                $("#dataTable").append(ss);
                            });
                        }  
                    }


                   
                    
                    //if (null != data.betDetailOddList && data.betDetailOddList.Count() > 0) {
                    //    $.each(data.betDetailOddList, function (i, n) {
                    //        var row = $('.odd-bet-row:eq(' + i + ')');
                    //        row.find('.BetCount').val(n.betCount);
                    //        row.find(".Id").val(n.id);
                    //        var selector = row.find('select.match');
                    //        var ids = [];
                    //        $.each(n.betDetailOddContentsList, function (j, k) {
                    //            ids.push(k.selectedMatchId);
                    //        });
                    //        selector.val(ids).trigger("change");
                    //    });
                    //}
                }

                //var length = 0;
                //if (null != data.betDetailOddList) {
                //    length = data.betDetailOddList.length - 1 < 0 ? 0 : data.betDetailOddList.length - 1;
                //}
                $("#OddId").val(id);

            }
        });

        var dialog = $("#editContentModal").removeClass('hide').dialog({
            modal: true,
            title: '导出',
            title_html: false,
            width: 1030,
            height: 860

        });
        //$("#editContentModal").modal("show");
    }
    //把数据库的list转换成js中的BetDetailOdd对象
    function ConvertToBetDetailOdds(list)
    { 
        var arr = new Array();
        if (undefined != list && null != list && list.length > 0)
        { 
            $.each(list, function (i, n) { 


                var c = [];
                var sp = 1;
                $.each(n.betDetailOddContentsList, function (j, k) { 
                    sp = sp * k.selectedOdds;
                    var d = {
                        WeekName: k.weekName,
                        BetDetailOddId: k.betDetailOddId,
                        SelectedMatchId: k.selectedMatchId,
                        SelectedResult: k.selectedResult,
                        FixOdds: k.fixOdds,
                        SelectedOdds: k.selectedOdds,
                        LotteryResults: k.lotteryResults
                    }
                    c.push(d);
                });

                var bet = {
                    Index: i,
                    Checked: true,
                    Content: getContentString(c),
                    SP: sp.toFixed(2),
                    BetCount: n.betCount,
                    BetDetailOddContents:c
                }
                arr.push(bet);
            });
            
        }
        return arr;
    }

    //数组最大公约数
    function MaxCommonDivisor(arr)
    {
        var a = arr[0];
        var b = arr[1];
        var c = GCD(a, b);
        for (var i = 2; i < arr.length; i++) {
            c = GCD(c, arr[i]);
        }
        return c;
    }
    function GCD(n,m)
    {
        if (n < m) {
            n = m + n;
            m = n - m;
            n = n - m;
        }
        if (m == 0) return n;
        return GCD(m, n % m);
    }

    ///保存选择得投注选项
    function SaveMatchContents() {
        var matchContents = GetMatchContents();

        var money = parseInt($("#a1").val()) * parseInt($("#a2").val());
        if (selectedSingleOdd.ownPurchaseMoney != 0 && selectedSingleOdd.ownPurchaseMoney != money)
        { 
            if  (!confirm("自购金额与预约金额不一致，确定要保存吗？"))  {
                return;
             } 
        }


        var id = parseInt($("#OddId").val());

        if (matchContents.BetDetailOddList.length == 0)
        { 
            layer.tips("你没有选择投注项", "#btnSave");
            return;
        }

        var postData = {
            dto: matchContents
        };

        //var postData = { oddId: id, betDetailOdds: matchContents };
        $.ajax({
            type: "Post",
            url: "/Odd/EditOddContents",
            data: postData,
            success: function (d) {
                if (d.success) {

                    $(".ui-dialog-titlebar-close").trigger("click");
                    LoadTables(1, 20);
                } else {
                    layer.tips(data.message, "#btnSave");
                };
            }
        });
    }


    ///获取要保存得投注内容
    function GetMatchContents() {

        var saveOdd = {
            Id: selectedSingleOdd.id,
            Divisor: parseInt($("#a2").val()),
            BetDetailOddList: [],
            OddSelectedMatchList:[]
        }
        var row = $("#dataTable tr:gt(0)");
        $.each(row, function (i, n) { 
            var index = $(n).find(".select-odd").val();
           
            var singleOdd = betDetailOdds.FirstOrDefault(function (x) { return x.Index == parseInt(index) });
            if (singleOdd.Checked != true)
            { 
                return true;
            }
            var beishu = parseInt($("#a2").val());
            var rowData = {
                Id: -1,
                OddId: $("#OddId").val(),
                BetCount: (parseInt($(n).find(".count").val()) * beishu),
                Remark: '',
                BetDetailOddContentsList: singleOdd.BetDetailOddContents
            };
            if (rowData.BetCount > 0) {
                saveOdd.BetDetailOddList.push(rowData);
            }
        });


        row = $(".matchData");
        var smatchs = new Array();
        $.each(row, function (i, n) {
            if ($(n).find(".match").val() != "") {
                var matchId = parseInt($(n).find(".match").val());
                var smatch = {
                    OddId: $("#OddId").val(),
                    SelectedMatchId: matchId,
                    Dan: ($(n).find(".dan").get(0).checked ? true : false),
                    SelectedJCHadOddId: -1,
                    SelectedJCHHadOddId: -1,
                    SelectedJCHAFUOddId: -1,
                    SelectedJCHadOdds: "",
                    SelectedJCHHadOdds: "",
                    SelectedJCHAFUOdds: ""
                }
                var trs = $(n).find("table tr");

                smatch.SelectedJCHadOddId = $(trs[0]).find("a").attr("bOddId");
                smatch.SelectedJCHHadOddId = $(trs[1]).find("a").attr("bOddId");
                smatch.SelectedJCHAFUOddId = $(trs[2]).find("a").attr("bOddId");


                $.each(trs, function (j, k) {
                    //只串让球不让球胜平负的选项，半全场跳过
                    if (j > 1) {
                        var tds = $(k).find("td");
                        if ($(tds[1]).hasClass("selected")) {
                            smatch.SelectedJCHAFUOdds += "7";
                        }
                        if ($(tds[3]).hasClass("selected")) {
                           
                            if (smatch.SelectedJCHAFUOdds.length > 0) {
                                smatch.SelectedJCHAFUOdds += ",";
                                }
                            smatch.SelectedJCHAFUOdds += "8";
                           
                        }
                    } else {
                        var tds = $(k).find("td");
                        if ($(tds[1]).hasClass("selected")) {

                            if (j == 0) {
                                smatch.SelectedJCHadOdds += "1";
                            } else if (j == 1) {
                                smatch.SelectedJCHHadOdds += "4";
                            }
                        }
                        if ($(tds[2]).hasClass("selected")) {

                            if (j == 0) {
                                if (smatch.SelectedJCHadOdds.length > 0) {
                                    smatch.SelectedJCHadOdds += ",";
                                }
                                smatch.SelectedJCHadOdds += "2";
                            } else if (j == 1) {
                                if (smatch.SelectedJCHHadOdds.length > 0) {
                                    smatch.SelectedJCHHadOdds += ",";
                                }
                                smatch.SelectedJCHHadOdds += "5";
                            }
                        }
                        if ($(tds[3]).hasClass("selected")) {
                            if (j == 0) {
                                if (smatch.SelectedJCHadOdds.length > 0) {
                                    smatch.SelectedJCHadOdds += ",";
                                }
                                smatch.SelectedJCHadOdds += "3";
                            } else if (j == 1) {
                                if (smatch.SelectedJCHHadOdds.length > 0) {
                                    smatch.SelectedJCHHadOdds += ",";
                                }
                                smatch.SelectedJCHHadOdds += "6";
                            }
                        }
                    }
                });
                if (smatch.SelectedJCHadOdds.length > 0 || smatch.SelectedJCHHadOdds.length > 0 || smatch.SelectedJCHAFUOdds.length>0) {
                    smatchs.push(smatch);
                }
            }
        });
        saveOdd.OddSelectedMatchList = smatchs;
        return saveOdd;

        //var row = $('.odd-bet-row');
        //$.each(row, function (i, n) {
        //    if (!$(n).is(':visible')) {
        //        //如果这个DIV是隐藏的 就跳过它
        //        return true;
        //    }

        //    var rowData = {
        //        Id: -1,
        //        OddId: $("#OddId").val(),
        //        BetCount: 0,
        //        Remark: '',
        //        BetDetailOddContentsList: []
        //    };
        //    rowData.Id = $(n).find('.Id').val();
        //    rowData.BetCount = $(n).find('.BetCount').val();
        //    var cs = $(n).find('select.odd').val();

        //    $.each(cs, function (m, k) {
        //        var values = k.split("|");
        //        var detailOdd = {
        //            Id: -1,
        //            BetDetailOddId: 0,
        //            WeekName: values[0],
        //            SelectedMatchId: values[1],
        //            SelectedResult: values[2],
        //            SelectedOdds: values[3],
        //            FixOdds: values[4] + ''
        //        };

        //        //var content = $.parseJSON(k);
        //        rowData.BetDetailOddContentsList.push(detailOdd);

        //    });
        //    if (rowData.BetCount > 0) {

        //        tetDetailOdd.push(rowData);
        //    }

        //});
    }


</script>
